package com.example.zhanglinan.vrdemo.fragment;

import android.content.Intent;
import android.os.SystemClock;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhanglinan.vrdemo.R;
import com.example.zhanglinan.vrdemo.activity.PanoActivity;
import com.example.zhanglinan.vrdemo.bean.PanoBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import static com.example.zhanglinan.vrdemo.bean.Pano.getImageItems;


/**
 * @author Schutzengel
 *         o(＞﹏＜)o
 *         create on 2017/1/27 14:41
 *         $desc$
 */
public class PanoFragmnet extends BaseFragment {

    private List<PanoBean> list;
    private MyListener myListener;
    private Adapter mAdapter;
    //线性布局
    //    private LinearLayoutManager mManager;
    //瀑布流
    private StaggeredGridLayoutManager mManager;
    private static final int TYPE_ITEM = 0;  //普通Item View
    private static final int TYPE_FOOTER = 1;  //底部FootView
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //上拉加载更多状态-默认为0
    private int load_more_status = 0;
    //是否刷新的标志
    private boolean mIsFresh;
    //记录图片高度的集合
    private List<Integer> picList;

    @Override
    public RecyclerView.LayoutManager getManager() {
//        mManager = new LinearLayoutManager(getActivity());
        mManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        return mManager;
    }

    @Override
    public RecyclerView.Adapter getAdapter() {
        list = getImageItems();
        myListener = new MyListener();
        mAdapter = new Adapter();
        picList = new ArrayList<>();
        return mAdapter;
    }

    private class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        //将上拉加载的布局添加到正中间
        @Override
        public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
            super.onViewAttachedToWindow(holder);
            ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
            if (layoutParams != null && layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
                //是否是最后一个
                if (holder.getLayoutPosition() == list.size()) {
                    StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                    p.setFullSpan(true);
                    mIsFresh = true;
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            //最后一个返回footview
            if (position + 1 == getItemCount()) {
                return TYPE_FOOTER;
            } else {
                return TYPE_ITEM;
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view;
            //根据不同类型创建不同对象
            if (viewType == TYPE_ITEM) {
                view = getActivity().getLayoutInflater().inflate(R.layout.fragment_pano, parent, false);
                return new PanoHolder(view);
            } else if (viewType == TYPE_FOOTER) {
                view = getActivity().getLayoutInflater().inflate(R.layout.foot_view, parent, false);
                return new FootHolder(view);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            //根据类型的不同 匹配不同的逻辑
            if (holder instanceof PanoHolder) {
                PanoHolder panoHolder = (PanoHolder) holder;
                panoHolder.fbPano.setImageURI(list.get(position).getUrl());
                panoHolder.tvPano.setText(list.get(position).getName());
                //将流布局的高度存到集合中，防止从新加载时，位置错乱的问题
                if (picList.size() <= position) {
                    picList.add((int) (200 + Math.random() * 400));
                }
                //获取params设置高度
                ViewGroup.LayoutParams params = panoHolder.fbPano.getLayoutParams();
                params.height = picList.get(position);
                panoHolder.fbPano.setLayoutParams(params);
                panoHolder.llPano.setTag(list.get(position));
                panoHolder.llPano.setOnClickListener(myListener);
            } else if (holder instanceof FootHolder) {
                FootHolder footHolder = (FootHolder) holder;
                switch (load_more_status) {
                    case PULLUP_LOAD_MORE:
                        footHolder.tvFt.setText("上拉加载更多");
                        break;
                    case LOADING_MORE:
                        footHolder.tvFt.setText("正在加载更多");
                        break;
                }
            }

        }

        @Override
        public int getItemCount() {
            return list.size() + 1;
        }

        class PanoHolder extends RecyclerView.ViewHolder {

            private final SimpleDraweeView fbPano;
            private final TextView tvPano;
            private final LinearLayout llPano;

            public PanoHolder(View itemView) {
                super(itemView);
                fbPano = (SimpleDraweeView) itemView.findViewById(R.id.fb_pano);
                tvPano = (TextView) itemView.findViewById(R.id.tv_pano);
                llPano = (LinearLayout) itemView.findViewById(R.id.ll_pano);
            }
        }

        //footview
        class FootHolder extends RecyclerView.ViewHolder {

            private final TextView tvFt;

            public FootHolder(View itemView) {
                super(itemView);
                tvFt = (TextView) itemView.findViewById(R.id.tv_ft);
            }
        }

    }

    class MyListener implements View.OnClickListener {
        Intent intent = new Intent(getActivity(), PanoActivity.class);

        @Override
        public void onClick(View v) {
            PanoBean bean = (PanoBean) v.getTag();
            intent.putExtra("url", bean.getUrl());
            intent.putExtra("name", bean.getName());
            intent.putExtra("mp3", bean.getMp3());
            getActivity().startActivity(intent);
        }
    }

    @Override
    public void onFresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(2000);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        onComplete();
                        Toast.makeText(getActivity(), "刷新完成", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();
    }

    @Override
    public void onAdd(RecyclerView recyclerView, int newState) {
        load_more_status = LOADING_MORE;
        //是手指离开状态 和 可以刷新
        if (newState == RecyclerView.SCROLL_STATE_IDLE && mIsFresh) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    load_more_status = PULLUP_LOAD_MORE;
                    SystemClock.sleep(2000);
                    list.addAll(list);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.notifyDataSetChanged();
                            Toast.makeText(getActivity(), "加载完成", Toast.LENGTH_SHORT).show();
                            //刷新后置为false
                            mIsFresh = false;
                        }
                    });
                }
            }).start();
        }
    }
}
